package components.base
{
	import flash.events.MouseEvent;
	
	import mx.events.FlexEvent;
	
	import spark.components.Button;
	
	import common.constants.AppConstants;
	
	import events.ObjectEvent;
	
	import framework.LangMgr;
	
	import interfaces.IDispose;
	import interfaces.IValidation;
	
	[Style(name="icon", type="Class", inherit="no")]
	public class BaseButton extends Button implements IDispose
	{
		/**
		 * 是否隐藏ICON
		 */
		[Bindable]
		public var iconVisible:Boolean = true;
		
		/**是否作为验证的触发按钮**/
		public var isValidateTrigger:Boolean;
		
		/**验证容器**/
		private var _validation:IValidation;
		public function setValidation(p_validation:IValidation):void
		{
			_validation = p_validation;
		}
		
		/**附带数据**/
		public var data:Object;
		
		/**
		 * 国际化标识
		 */
		private var _locale:String;
		public function get locale():String
		{
			return _locale;
		}
		public function set locale(p_str:String):void
		{
			_locale = p_str;
			resourcesChanged();
		}
		
		public function BaseButton()
		{
			super();
			buttonMode = true;
			addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
		}
		
		/**
		 * 控件创建完成后调用初始化信息
		 */
		private function creationCompleteHandler(p_evt:FlexEvent):void
		{
			removeEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
			if(styleName == null)
				styleName = "button";
			if(isValidateTrigger)
				dispatchEvent(new ObjectEvent(AppConstants.GET_VALIDATION, setValidation, true));
		}
		
		override protected function clickHandler(event:MouseEvent):void
		{
			//验证
			if(isValidateTrigger && _validation){
				var result:Array = _validation.validate();
				if(result && result.length > 0){
					event.stopImmediatePropagation();
					return;
				}
			}
			super.clickHandler(event);
		}
		
		/**
		 * 实现国际化
		 */
		override protected function resourcesChanged():void
		{
			super.resourcesChanged();
			if(_locale)
				label = LangMgr.getResourceByLocaleKey(_locale);
		}
		
		public function dispose():void
		{
			
		}
	}
}